﻿/// <Summary> 
/// $Author: Nikhil Chinchwade 

/// $Creation Date : 
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MindTree.PerfWorkbench.Azure.Monitor.Core;
using System.Collections;
using System.IO;

namespace MindTree.PerfWorkbench.Azure.Monitor
{
    /// <summary>
    /// Interaction logic for ConfigureMonCounters.xaml
    /// </summary>
    public partial class ConfigureMonCounters : Window
    {
        public bool IsOK = false;

        private IList<PerfCounter> selectedCounters = new List<PerfCounter>();

        public IList<PerfCounter> SelectedCounters
        {
            get
            {
                return selectedCounters;
            }
            set
            {
                selectedCounters = value;
            }
        }

        /// <summary>
        /// Constructor accepts
        /// </summary>
        /// <param name="paramConfiguredCounters"></param>
        public ConfigureMonCounters(IList<PerfCounter> paramConfiguredCounters)
        {
            InitializeComponent();
            foreach (PerfCounter cntr in paramConfiguredCounters)
            {
                SelectedCounters.Add(new PerfCounter(cntr));
            }
            //SelectedCounters = paramConfiguredCounters;
            lstBoxSelectedCounters.ItemsSource = SelectedCounters;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            IsOK = true;
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            IsOK = false;
            Close();
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnRemoveSelectedCntrs_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnRemoveAllCntrs_Click(object sender, RoutedEventArgs e)
        {

        }

        private void imgClear_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete all counters?",
                    "Delete confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                SelectedCounters.Clear();
                lstBoxSelectedCounters.Items.Refresh(); ;
            }
            
        }

        private void imgRemoveSelected_MouseDown(object sender, MouseButtonEventArgs e)
        {

            if (MessageBox.Show("Do you really want to delete selected counters?",
                    "Delete confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                IList selectedCounters = lstBoxSelectedCounters.SelectedItems;
                foreach (PerfCounter cntr in selectedCounters)
                {
                    SelectedCounters.Remove(cntr);
                }
                lstBoxSelectedCounters.Items.Refresh();
            }
        }

        private void imgImport_MouseDown(object sender, MouseButtonEventArgs e)
        {

            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.DefaultExt = ".csv";
            dialog.Filter = "Counter File (*.csv, *.ctr)|*.csv, *.ctr | All Files | *.*";

            if (dialog.ShowDialog() == true)
            {
                string filename = dialog.FileName;

                StreamReader filereader = new StreamReader(filename);
                string cntr = filereader.ReadLine();

                while (!String.IsNullOrEmpty(cntr))
                {
                    PerfCounter ctr = PerfCounter.GetCounterFromString(null, cntr.Substring(1, cntr.Length - 1));
                    if (!SelectedCounters.Contains(ctr))
                    {
                        SelectedCounters.Add(ctr);
                    }
                    cntr = filereader.ReadLine();
                }
                filereader.Close();
                lstBoxSelectedCounters.Items.Refresh();
            }

        }

        private void imgExport_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".csv";
            dlg.Filter = "Counter File (*.csv, *.ctr)|*.csv, *.ctr | All Files | *.*";
            if (dlg.ShowDialog() == true)
            {
                string filename = dlg.FileName;

                StringBuilder countersString = new StringBuilder();
                foreach (PerfCounter cntr in SelectedCounters)
                {
                    countersString.AppendLine("\"" + cntr.ToString() + "\"");
                }

                StreamWriter writer = new StreamWriter(filename);
                writer.Write(countersString.ToString());
                writer.Close();
            }
        }

        private void btnAddCounter_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(txtBoxCounterAdd.Text))
            {
                return;
            }
            PerfCounter cntr = null;
            try
            {
                cntr = PerfCounter.GetCounterFromString(null, txtBoxCounterAdd.Text);
            }
            catch (Exception ex)
            {
                return;
            }

            if (!SelectedCounters.Contains(cntr))
            {
                SelectedCounters.Add(cntr);
                lstBoxSelectedCounters.Items.Refresh();
                txtBoxCounterAdd.Clear();
            }
            else
            {
                //already exist
            }
        }
    }
}
